﻿/**********************************************************************/
/*                                                                    */
/*                   embDocumentInhalator                             */
/*             http://embdocumentinhalator.codeplex.com/              */
/*                                                                    */
/*               (c) Copyright 2010 Ilija Injac         .             */
/*                                                                    */
/*  This source is subject to the CDDL License                        */
/*  http://embdocumentinhalator.codeplex.com/license                  */
/*                                                                    */
/*                                                                    */
/**********************************************************************/
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace embDocumentInhalator
{
    /// <summary>
    /// Helper functions for image conversion
    /// Most of the code was taken from the
    /// internet. This is only a temporary solution
    /// and should be replaced by the native imaging
    /// functions build in in via, or by a professional
    /// imaging library.
    /// </summary>
    /// This element was inserted by injac on machine PLATINCHECKER.
    /// 26.01.2011,23:14
    public class BitmapConversions
    {
        /// <summary>
        /// Converts the the image to 16-bits per pixel.
        /// </summary>
        /// <param name="img">The img.</param>
        /// <returns></returns>
        /// This element was inserted by injac on machine PLATINCHECKER.
        /// 26.01.2011,23:14
        public Bitmap ConvertTo16bpp(Image img)
        {
            var bmp = new Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
            var gr = Graphics.FromImage(bmp);
            gr.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height)); return bmp;
        }

        /// <summary>
        /// Converts an image to a byte array.
        /// </summary>
        /// <param name="Bild">The bild.</param>
        /// <param name="Bildformat">The bildformat.</param>
        /// <returns></returns>
        /// This element was inserted by injac on machine PLATINCHECKER.
        /// 26.01.2011,23:15
        public byte[] ImageToByteArray(Image Bild, ImageFormat Bildformat)
        {
            MemoryStream IS = new MemoryStream();
            Bild.Save(IS, Bildformat);
            IS.Flush();
            return IS.ToArray();
        }

        /// <summary>
        /// Converts a byte array to an image.
        /// </summary>
        /// <param name="imageData">The image data.</param>
        /// <returns></returns>
        /// This element was inserted by injac on machine PLATINCHECKER.
        /// 26.01.2011,23:15
        public Image ByteArrayToImage(byte[] imageData)
        {
            MemoryStream IS = new MemoryStream(imageData);
            Image m = Image.FromStream(IS);
            return m;
        }

        /// <summary>
        /// Converts a bitmap to grayscale.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <returns></returns>
        /// This element was inserted by injac on machine PLATINCHECKER.
        /// 26.01.2011,23:15
        public Bitmap ConvertToGrayscale(Bitmap source)
        {
            Bitmap bm = new Bitmap(source.Width, source.Height);

            for (int y = 0; y < bm.Height; y++)
            {
                for (int x = 0; x < bm.Width; x++)
                {
                    Color c = source.GetPixel(x, y);

                    int luma = (int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11);

                    bm.SetPixel(x, y, Color.FromArgb(luma, luma, luma));
                }
            }

            return bm;
        }

    }
}
